#ifndef DATAOUT_H
#define DATAOUT_H
#include <string>
using namespace std;
#include "Max.h"
#include "resource.h"
#include "istdplug.h"
#include "stdmat.h"
#include "decomp.h"
#include "shape.h"
#include "interpik.h"
#include "dataStructures.h"
#include "asciitok.h"

#define CLASS_ID		(TCHAR*)"worldExport"
#define COPYRIGHT_ID	(TCHAR*)"Dacre Denny"
#define STR_LONGDESC	(TCHAR*)"World Exporter for ProRider"
#define STR_SHORTDESC	CLASS_ID
#define STR_PRGRESSINIT	(TCHAR*)"Exporting world..."
#define WORLDEXP_CLASS_ID	Class_ID(0x85548e0f, 0x4a26450d)
#define VERSION			200			// Version number * 100
#define STR_DEFAULTFILE	"D:\\ProRider\\Program\\PRide\\maps\\world.bwf"
#define STR_DEFAULTMODELFILE	"D:\\ProRider\\Program\\PRide\\models\\"
#define EXPTIME 0
#define STR_DEFAULTWORLDNAME	"WorldPeak"
#define STR_DEFAULTWORLDDESC	"WorldPeak Debug world!"

#define FAIL_TEXTURE_NAME	"noshader"

extern VOID showExportDialog();
extern VOID showModelDialog();

extern Interface *g_pIp;


class MtlKeeper {
public:
	BOOL	AddMtl(Mtl* mtl);
	int		GetMtlID(Mtl* mtl);
	int		Count();
	Mtl*	GetMtl(int id);

	Tab<Mtl*> mtlTab;
};

class WorldExp{

private:
	class Exception{

	public:
		string msg;
		Exception(string msg){
			this->msg=msg;
		}
	};

	bool		isWorldProcessDone;
	bool		isCollisionDataDone;

	FILE*		pFilePtr;
	TCHAR		szFmtStr[16];
	INode* pWorldMesh;
	MtlKeeper	mtlList;

	void preprocess(INode *pNode);
	void processNode(INode* node);
	void aquireMaterials();

	int getNodeType(INode *pNode);

	void writeShader(Mtl* mtl);
	void writeEntities();
	void writeHeader();
	void writeGlobalInfo();
	void writeFog();
	void writeEnvironment();

	int	fogSize;
	int	sunSize;
	int	weatherSize;
	int	entitySize;
	int	meshSize;

	void writeShaders();
	void writeWorldMesh(); 
	void writeCollisionModel(INode* node);
	void writeLightEntity(INode* node);
	void writeGenricEntity(INode* node);

	void writeMeshGroup(INode* node, int matID);
	TCHAR* GetMapID(Class_ID cid, int subNo);

	BOOL CheckForAndExportFaceMap(Mtl* mtl, Mesh* mesh, int indentLevel); 

	BOOL TMNegParity(Matrix3 &m);
	TriObject* GetTriObjectFromNode(INode *node, TimeValue t);

public:
	
	WorldExp(VOID);
	~WorldExp(VOID);

	
	exportPacket data;

	void doExport();

};
class ModelExp{

private:
	class Exception{

	public:
		string msg;
		Exception(string msg){
			this->msg=msg;
		}
	};

	FILE*		pFilePtr;
	INode* pMesh; 
	void getShader();
	void writeMesh(); 
	void writeHeader();

	string skinName;
	int frames;

	BOOL CheckForAndExportFaceMap(Mtl* mtl, Mesh* mesh, int indentLevel); 

	BOOL TMNegParity(Matrix3 &m);
	TriObject* GetTriObjectFromNode(INode *node, TimeValue t);

public:
	
	ModelExp(VOID);
	~ModelExp(VOID);
	void doExport(string filename,int numFrames);
	void doExport(INode* pNode,string filename,int numFrames);

};

#endif

